#ifndef NP_SYSFUNTEST_H
#define NP_SYSFUNTEST_H

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <config/RuleManager.h>
#include <config/Config.h>
#include <gather/Gather.h>
#include <dao/Connections.h>
#include <dao/DaoArticle.h>
#include <mysql/mysql.h>
#include <boost/filesystem/v3/operations.hpp>

using namespace std;
using namespace ruleconfiguration;
using namespace npexception;
using namespace articleInfo;

namespace sysfuntest
{
void inputProtect(int argc)
{
  throwExIf(argc == 1, RULEINFO_TEST_INVALID_INPUT);
}

void putItInFile(const char* ptr, int len)
{
	std::ofstream o("./test.txt");
	o.write(ptr, len);
	o.close();
}

template<typename T>
void printIt(const T& t)
{
  cout << t << endl;
}

void getRuleInfoTest(int argc, char* argv[])
{
  inputProtect(argc);
  RegexInfo* tmp = RuleManager::getInstance()->getRuleInfo(argv[1], argv[2]);
  printIt(tmp->FilterPattern);
  printIt(tmp->Method);
  printIt(tmp->Options);
  printIt(tmp->Pattern);
}

void getPic()
{
  int receiveLength = 0;
  char* tmp = Gather::getInstance()->collect("http://portrait6.sinaimg.cn/1591701013/blog/180", receiveLength);
  cout << tmp << endl;
}

//void mysqlTest()
//{
//  MYSQL* conn = mysql_init((MYSQL*)0);
//  MYSQL* tmp = mysql_real_connect(conn, "127.0.0.1", "remoteuser", "123456", "nptest", 3306, NULL, 0);
//  string sql = "select max(articleid) as  articleid from jieqi_article_article;";
//  int flag = mysql_real_query(conn, sql.c_str(), sql.length());
//  if (flag != 0)
//  {
//    cout << mysql_error(conn) << endl;
//  }
//  MYSQL_RES* res = mysql_store_result(conn);
//  MYSQL_ROW row = mysql_fetch_row(res);
//  mysql_free_result(res);
//}

void daoTest()
{
//  DaoArticle daoArticle;
//  daoArticle.setConn(Connections::getInstance()->getConnection("nptest"));

//  //getArticle
//  ArticleInfo articleInfo;
//  bool flag = daoArticle.getArticle("一等家丁", articleInfo);
//  throwExIf(flag == false, "getArticle error.");
//  printIt("///////////////// getArticle ////////////////");
//  printIt(articleInfo.downloadlink.c_str());
//  printIt(articleInfo.allvote.c_str());
//  printIt(articleInfo.articletype);
//  printIt("///////////////// getArticle ////////////////");

//  //getArticleCount
//  printIt("///////////////// getArticleListCount ////////////////");
//  int count = daoArticle.getArticleListCount();
//  printIt(count);
//  printIt("///////////////// getArticleListCount ////////////////");

//  //getArticleList
//  printIt("///////////////// getArticleList ////////////////");
//  ArticleInfo articleInfos[count];
//  count = daoArticle.getArticleList("0", articleInfos);
//  printIt(count);
//  printIt("///////////////// getArticleList ////////////////");

//  //getArticleMaxID
//  string maxId;
//  printIt("///////////////// getArticleMaxID ////////////////");
//  daoArticle.getArticleMaxID(maxId);
//  printIt(maxId.c_str());
//  printIt("///////////////// getArticleMaxID ////////////////");

}

void saveAPic(const char* link)
{
  std::ofstream o("./pic.jpg");
  if (o.is_open() == false) { throw "file open error."; }
  int length = 0;
  char* tmp = Gather::getInstance()->collectAPic(link, length);

  o.write(tmp, length);
  o.close();
}

void boostFileSysTest()
{
  bool flag = boost::filesystem3::is_directory("/");
  if (flag == false)
  {
    throw "is_directory error.";
  }
}

void gatherTest(const char* req)
{
  char* tmp = Gather::getInstance()->collect(req);
  putItInFile(tmp, strlen(tmp));
}
}

#endif // NP_SYSFUNTEST_H
